import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yqys_target/utils/yq_colors.dart';
import 'package:yqys_target/widgets/form_list_item.dart';
import 'package:yqys_target/pages/organization/controllers/organization_auth_controller.dart';
import 'package:yqys_target/widgets/xh_auto_height_text_field.dart';

class OrganizationInfoPage extends StatelessWidget {
  const OrganizationInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.find<OrganizationAuthController>();

    return SingleChildScrollView(
      padding: const EdgeInsets.all(16),
      child: DefaultTextStyle(
        style: TextStyle(fontSize: 15, color: YQColors.primary),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 负责人
                _PersonInCharge(),
                const SizedBox(height: 12),
                // 创建人信息
                Container(
                  color: Colors.white,
                  padding: const EdgeInsets.only(left: 16, right: 16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding:
                            const EdgeInsets.only(left: 16, top: 0, bottom: 10),
                        child: Text(
                          "创建人",
                          style: TextStyle(
                            fontSize: 15,
                            color: Colors.grey[600],
                          ),
                        ),
                      ),
                      const _CreatorInfoCard(
                        name: "A先生",
                        phone: "13254256324",
                        idCard: "422852423624563245",
                        avatar: null,
                      )
                    ],
                  ),
                ),
                const SizedBox(height: 16),
                // 法人信息
                const _LegalPersonTitle(),
                const SizedBox(height: 24),
                // 核心成员
                const _CoreMemberTitleWidget(),
                const SizedBox(height: 12),
                // 核心成员列表
                Column(
                  children: [
                    FormListItem(
                      required: false,
                      leading: _CoreMembersLeading(name: "核心成员1"),
                      trailing: XHAutoHeightTextField(
                        maxLength: 11,
                        width: 200,
                        placeholder: "请输入手机号",
                        onChanged: (value) {},
                      ),
                    ),
                    FormListItem(
                      required: false,
                      leading: _CoreMembersLeading(name: "核心成员2"),
                      trailing: XHAutoHeightTextField(
                        maxLength: 11,
                        width: 200,
                        placeholder: "请输入手机号",
                        onChanged: (value) {},
                      ),
                    ),
                    FormListItem(
                      required: false,
                      leading: _CoreMembersLeading(name: "核心成员3"),
                      trailing: XHAutoHeightTextField(
                        maxLength: 11,
                        width: 200,
                        placeholder: "请输入手机号",
                        onChanged: (value) {},
                      ),
                    ),
                    FormListItem(
                      required: false,
                      leading: _CoreMembersLeading(name: "核心成员4"),
                      trailing: XHAutoHeightTextField(
                        maxLength: 11,
                        width: 200,
                        placeholder: "请输入手机号",
                        onChanged: (value) {},
                      ),
                    ),
                    FormListItem(
                      required: false,
                      leading: _CoreMembersLeading(name: "核心成员5"),
                      trailing: XHAutoHeightTextField(
                        maxLength: 11,
                        width: 200,
                        placeholder: "请输入手机号",
                        onChanged: (value) {},
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class _LegalPersonTitle extends StatelessWidget {
  const _LegalPersonTitle({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding:
                const EdgeInsets.only(left: 16, top: 0, bottom: 10),
            child: Text(
              "法人",
              style: TextStyle(
                fontSize: 15,
                color: Colors.grey[600],
              ),
            ),
          ),
          _CreatorInfoCard(
              name: "A先生",
              phone: "13254256324",
              idCard: "422852423624563245",
              avatar: null)
        ],
      ),
    );
  }
}

class _CoreMemberTitleWidget extends StatelessWidget {
  const _CoreMemberTitleWidget({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "核心成员",
            style: TextStyle(
              fontSize: 15,
              color: YQColors.primary,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 5),
          Text(
            "可添加核心成员",
            style: TextStyle(
              fontSize: 12,
              color: Colors.grey[600],
            ),
          ),
        ],
      ),
    );
  }
}

class _CreatorInfoCard extends StatelessWidget {
  final String? name;
  final String? phone;
  final String? idCard;
  final String? avatar;
  const _CreatorInfoCard({
    super.key,
    required this.name,
    required this.phone,
    required this.idCard,
    required this.avatar,
  });

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(10),
      child: Container(
        child: Column(children: [
          FormListItem(
            backgroundColor: Colors.grey[100],
            required: false,
            showArrow: false,
            title: "头像",
            hideDivider: true,
            trailing: SizedBox(
              child: Container(
                width: 35,
                height: 35,
                decoration: avatar != null
                    ? BoxDecoration(
                        color: Colors.grey[100],
                        borderRadius: BorderRadius.circular(20),
                      )
                    : BoxDecoration(
                        color: Colors.orange[100],
                        borderRadius: BorderRadius.circular(20),
                      ),
                child: ClipOval(
                  child: avatar != null
                      ? Image.network(avatar!)
                      : Icon(Icons.person, color: Colors.orange),
                ),
              ),
            ),
          ),
          FormListItem(
            backgroundColor: Colors.grey[100],
            required: false,
            showArrow: false,
            title: "姓名",
            hideDivider: true,
            trailing: SizedBox(
              child: Text(
                name ?? "",
                style: TextStyle(color: Colors.grey[600]),
              ),
            ),
          ),
          FormListItem(
            backgroundColor: Colors.grey[100],
            required: false,
            showArrow: false,
            title: "手机号",
            hideDivider: true,
            trailing: SizedBox(
              child: Text(
                phone ?? "",
                style: TextStyle(color: Colors.grey[600]),
              ),
            ),
          ),
          FormListItem(
            backgroundColor: Colors.grey[100],
            required: false,
            showArrow: false,
            title: "身份证",
            hideDivider: true,
            trailing: SizedBox(
              child: Text(
                idCard ?? "",
                style: TextStyle(color: Colors.grey[600]),
              ),
            ),
          ),
        ]),
      ),
    );
  }
}

class _PersonInCharge extends StatelessWidget {
  const _PersonInCharge({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                "*",
                style: TextStyle(
                  fontSize: 18,
                  color: Colors.red,
                  fontWeight: FontWeight.bold,
                ),
              ),
              Text(
                "负责人",
                style: TextStyle(
                  fontSize: 18,
                  color: YQColors.primary,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
          const SizedBox(height: 6),
          Text(
            "可以上传1个负责人即可",
            style: TextStyle(
              fontSize: 13,
              color: YQColors.secondary,
            ),
          ),
          SizedBox(height: 10),
          Divider(
            height: 1,
            thickness: 0.5,
            indent: 0,
            endIndent: 16,
          )
        ],
      ),
    );
  }
}

class _CoreMembersLeading extends StatelessWidget {
  final String name;
  const _CoreMembersLeading({
    super.key,
    required this.name,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          name,
          style: TextStyle(
            fontSize: 13,
            color: YQColors.secondary,
          ),
        ),
        SizedBox(height: 6),
        Text(
          "手机号",
          style: TextStyle(
            color: YQColors.primary,
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
      ],
    );
  }
}
